package com.itgungnir.diyshuo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.itgungnir.diyshuo.activity.R;
import com.itgungnir.diyshuo.bean.GoodsBean;

import java.util.List;

/**
 * 展示商品列表的ListView的适配器类
 */
public class ListViewAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsBean> list;

    private LayoutInflater inflater;
    private ViewHolder holder;

    public ListViewAdapter(Context context, List<GoodsBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listitem_listview, null);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.find_listviewitem_iv_image);
            holder.title = (TextView) convertView.findViewById(R.id.find_listviewitem_tv_title);
            holder.love = (TextView) convertView.findViewById(R.id.find_listviewitem_tv_love);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GoodsBean goods = list.get(position);
        Glide.with(context).load(goods.getCoverpath()).placeholder(R.mipmap.img_listview_image).into(holder.image);
        holder.title.setText(goods.getSubject());
        holder.love.setText(goods.getViews());
        return convertView;
    }

    private class ViewHolder {
        ImageView image;
        TextView title;
        TextView love;
    }
}